﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Config;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;
using System.Text;

namespace CNVP.JShop.Web.Admin.Template
{
    public partial class TemplateExport : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UnitExport("Default");
                string _path = Request.Params["path"];
                if (string.IsNullOrEmpty(_path))
                {
                    _path = @"~/Template/Default/";
                }
                string FileName = "Template.zip";
                string FilePath = UIConfig.InstallDir + "Config/Cache/TempFile/";
                string[] FileProperties = new string[2];
                FileProperties[0] = Server.MapPath(_path);
                FileProperties[1] = Server.MapPath(FilePath + FileName);
                try
                {
                    ZipClass.ZipFolder(FileProperties);
                }
                catch
                {
                    LogHelper.Write("模板文件打包失败", "请检查模板路径是否正确！");
                }
                Response.Redirect(FilePath + FileName);
                Response.End();
            }
        }
        /// <summary>
        /// 导出单元
        /// </summary>
        /// <param name="ProjectName"></param>
        public void UnitExport(string ProjectName)
        {
            StringBuilder Str = new StringBuilder();
            Str.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
            Str.Append("<cnvp>\r\n");
            Str.Append("<version>1.0</version>\r\n");
            Str.Append("<copyright>温州市捷点信息技术有限公司</copyright>\r\n");

            var model_UnitsInfo = new BLL.CNVP_UnitsInfo().GetAll_CNVP_UnitsInfo("ProjectName= '" + ProjectName + "'");

            foreach (Model.CNVP_UnitsInfo m in model_UnitsInfo)
            {
                Str.Append("<unit>\r\n");
                Str.Append("<styleid>" + m.StyleID.ToString() + "</styleid>\r\n");
                Str.Append("<unitname>" + m.UnitName.ToString() + "</unitname>\r\n");
                Str.Append("<unithtml><![CDATA[" + m.UnitHTML.ToString() + "]]></unithtml>\r\n");
                Str.Append("</unit>\r\n");
            }
            Str.Append("</cnvp>");
            FileUtils.SaveFile(Str.ToString(), HttpContext.Current.Server.MapPath(UIConfig.InstallDir + "Template/" + ProjectName + "/Style.xml"));
        }
    }
}